package com.china08.yxyapi.api.comm.bigfileupload;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.entity.db.continuedu.YxyContinueduLessonsVideo;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsVideo;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLessonsVideoRepository;
import com.china08.yxyapi.repository.db.continuedu.YxyContinueduLessonsVideoRepository;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class BigFileUploadJudgeMd5Controller {

    @Autowired
    YxyLessonsVideoRepository videoRepository;

    @Autowired
    YxyContinueduLessonsVideoRepository continueduVideoRepository;

    @ApiOperation("教师上传课程使用,区域课程使用")
    @RequestMapping(value = "/api_yw/comm/bigfile/upload/judge/md5", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        RespModel respModel = new RespModel();
        String md5 = reqModel.md5;
        YxyLessonsVideo video = videoRepository.findByMd5(md5);
        if (video != null) {
            respModel.has = true;
            respModel.videoId = video.getId();
        }
        return respModel;
    }

    @ApiOperation("继续教育上传课程使用")
    @RequestMapping(value = "/api_yw/comm/bigfile/continuedu/upload/judge/md5", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel proModel(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {

        // 模拟网速慢
        // try {
        // Thread.sleep(3000);
        // } catch (Exception e) {
        // }
        RespModel respModel = new RespModel();
        String md5 = reqModel.md5;
        YxyContinueduLessonsVideo video = continueduVideoRepository.findByMd5(md5);
        if (video != null) {
            respModel.has = true;
            respModel.videoId = video.getId();
        }
        return respModel;
    }

    protected static class ReqModel extends YxyApiReqModel {
        public String md5;

        @Override
        public void autoCheck() {
            Assert.isTrue(StringUtils.isNotBlank(md5), "视频md5不能为空");
        }

    }

    protected static class RespModel extends YxyApiRespModel {
        public boolean has;
        public String videoId;
    }
}
